package com.example.wenhua.service.impl;

import com.example.wenhua.entity.Course;
import com.example.wenhua.mapper.CourseMapper;
import com.example.wenhua.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 课程业务逻辑实现类
 */
@Service
public class CourseServiceImpl implements CourseService {
    
    @Autowired
    private CourseMapper courseMapper;
    
    @Override
    public List<Course> getAllCourses() {
        return courseMapper.findAll();
    }
    
    @Override
    public List<Course> getCoursesWithFilter(String cno, String cname, String teacher) {
        return courseMapper.findWithFilter(cno, cname, teacher);
    }
    
    @Override
    public Course getCourseByCno(String cno) {
        return courseMapper.findByCno(cno);
    }
    
    @Override
    public List<Course> getCoursesByTeacher(String teacher) {
        return courseMapper.findByTeacher(teacher);
    }
    
    @Override
    @Transactional
    public boolean addCourse(Course course) {
        // 检查课程号是否已存在
        if (courseMapper.findByCno(course.getCno()) != null) {
            throw new RuntimeException("课程号已存在：" + course.getCno());
        }
        return courseMapper.insert(course) > 0;
    }
    
    @Override
    @Transactional
    public boolean updateCourse(Course course) {
        return courseMapper.update(course) > 0;
    }
    
    @Override
    @Transactional
    public boolean deleteCourse(String cno) {
        // 检查是否有学生选了这门课
        Integer studentCount = courseMapper.getStudentCountByCno(cno);
        if (studentCount > 0) {
            throw new RuntimeException("该课程已有学生选课，无法删除");
        }
        return courseMapper.delete(cno) > 0;
    }
    
    @Override
    public Map<String, Object> getCourseDetailByCno(String cno) {
        Course course = courseMapper.findByCno(cno);
        if (course == null) {
            return null;
        }
        
        Map<String, Object> result = new HashMap<>();
        result.put("cno", course.getCno());
        result.put("cname", course.getCname());
        result.put("teacher", course.getTeacher());
        result.put("credit", course.getCredit());
        result.put("studentCount", courseMapper.getStudentCountByCno(cno));
        result.put("averageGrade", courseMapper.getAverageGradeByCno(cno));
        result.put("failCount", courseMapper.getFailCountByCno(cno));
        
        return result;
    }
    
    @Override
    public Map<String, Object> getCourseStatsByCno(String cno) {
        Course course = courseMapper.findByCno(cno);
        if (course == null) {
            return null;
        }
        
        Map<String, Object> stats = new HashMap<>();
        Integer enrollmentCount = courseMapper.getStudentCountByCno(cno);
        BigDecimal averageScore = courseMapper.getAverageGradeByCno(cno);
        Integer failCount = courseMapper.getFailCountByCno(cno);
        
        stats.put("enrollmentCount", enrollmentCount != null ? enrollmentCount : 0);
        stats.put("averageScore", averageScore != null ? averageScore : BigDecimal.ZERO);
        
        // 计算及格率
        if (enrollmentCount != null && enrollmentCount > 0 && failCount != null) {
            double passRate = ((double)(enrollmentCount - failCount) / enrollmentCount) * 100;
            stats.put("passRate", BigDecimal.valueOf(passRate));
        } else {
            stats.put("passRate", BigDecimal.ZERO);
        }
        
        return stats;
    }
    
    @Override
    public BigDecimal getCourseAverageGrade(String cno) {
        return courseMapper.getAverageGradeByCno(cno);
    }
    
    @Override
    public List<Map<String, Object>> getAllCoursesAverageGrade() {
        List<Course> courses = courseMapper.findAll();
        return courses.stream().map(course -> {
            Map<String, Object> result = new HashMap<>();
            result.put("cno", course.getCno());
            result.put("cname", course.getCname());
            result.put("teacher", course.getTeacher());
            result.put("averageGrade", courseMapper.getAverageGradeByCno(course.getCno()));
            return result;
        }).collect(Collectors.toList());
    }
    
    @Override
    public BigDecimal getOverallAverageScore() {
        return courseMapper.getOverallAverageScore();
    }
}